/*
 * @Description  : RegExp
 * @Autor        : 杜康
 * @Date         : 2021-08-14 09:34:59 +0800
 * @LastEditors  : 杜康
 * @LastEditTime : 2021-08-14 22:14:35 +0800
 */

/**
 * @description: 校验电子邮箱格式
 * @param { String } str
 * @return { Boolean }
 * @author: 杜康
 * @Date: 2021-08-14 09:55:07 +0800
 */
const isEmail = (str) => {
    let reg = new RegExp(
        "^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$"
    );
    if (reg.test(str)) {
        return true;
    } else {
        return false;
    }
}

/**
 * @description: 判断是否为邮箱地址
 * @param { String } str
 * @return { Boolean }
 * @author: 杜康
 * @Date: 2021-08-14 09:54:46 +0800
 */
const isEmail2 = (str) => {
    return /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/.test(str);
}

/**
 * @description: 校验电话号码格式
 * @param { String } str
 * @return { Boolean }
 * @author: 杜康
 * @Date: 2021-08-14 09:54:24 +0800
 */
const isTelCode = (str) => {
    let num = Number(str);
    let regExp = new RegExp("^[0-9]{11}$");
    let isChinaPhoneLegalRegExp = new RegExp(
        "^((13[0-9])|(14[5,7,9])|(15[0-3,5-9])|(166)|(17[3,5,6,7,8])|(18[0-9])|(19[8,9]))\\d{8}$"
    );
    if (regExp.test(num) && num != "" && isChinaPhoneLegalRegExp.test(num)) {
        return true;
    } else {
        return false;
    }
}

/**
 * @description: 判断是否为手机号
 * @param { String|Number } str
 * @return { Boolean }
 * @author: 杜康
 * @Date: 2021-08-14 09:53:31 +0800
 */
const isPhoneNum = (str) => {
    return /^(\+?0?86\-?)?1[3456789]\d{9}$/.test(str);
}

/**
 * @description: 身份证号
 * @param { String } cid
 * @return { Boolean }
 * @author: 杜康
 * @Date: 2021-08-14 09:52:52 +0800
 */
const isCnNewID = (cid) => {
    let arrExp = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]; // 加权因子
    let arrValid = [1, 0, "X", 9, 8, 7, 6, 5, 4, 3, 2]; // 校验码
    let reg =
        /^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/;
    if (reg.test(cid)) {
        let sum = 0;
        let idx;
        for (let i = 0; i < cid.length - 1; i++) {
            // 对前17位数字与权值乘积求和
            sum += parseInt(cid.substr(i, 1), 10) * arrExp[i];
        }
        // 计算模（固定算法）
        idx = sum % 11;
        // 检验第18为是否与校验码相等,此处只能用==，可能因为类型不一样
        return arrValid[idx] == cid.substr(17, 1).toUpperCase();
    } else {
        return false;
    }
}

/**
 * @description: 判断是否为身份证号
 * @param { String|Number } str
 * @return { Boolean }
 * @author: 杜康
 * @Date: 2021-08-14 09:51:54 +0800
 */
const isIdCard = (str) => {
    return /^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/.test(
        str
    );
};

/**
 * @description: 严格的身份证校验
 * @param {*} sId
 * @return {*}
 * @author: 杜康
 * @Date: 2021-08-14 09:50:58 +0800
 */
const isCardID = (sId) => {
    if (!/(^\d{15}$)|(^\d{17}(\d|X|x)$)/.test(sId)) {
        console.log("你输入的身份证长度或格式错误");
        return false;
    }
    //身份证城市
    let aCity = {
        11: "北京",
        12: "天津",
        13: "河北",
        14: "山西",
        15: "内蒙古",
        21: "辽宁",
        22: "吉林",
        23: "黑龙江",
        31: "上海",
        32: "江苏",
        33: "浙江",
        34: "安徽",
        35: "福建",
        36: "江西",
        37: "山东",
        41: "河南",
        42: "湖北",
        43: "湖南",
        44: "广东",
        45: "广西",
        46: "海南",
        50: "重庆",
        51: "四川",
        52: "贵州",
        53: "云南",
        54: "西藏",
        61: "陕西",
        62: "甘肃",
        63: "青海",
        64: "宁夏",
        65: "新疆",
        71: "台湾",
        81: "香港",
        82: "澳门",
        91: "国外",
    };
    if (!aCity[parseInt(sId.substr(0, 2))]) {
        console.log("你的身份证地区非法");
        return false;
    }

    // 出生日期验证
    let sBirthday = (
            sId.substr(6, 4) +
            "-" +
            Number(sId.substr(10, 2)) +
            "-" +
            Number(sId.substr(12, 2))
        ).replace(/-/g, "/"),
        d = new Date(sBirthday);
    if (
        sBirthday !=
        d.getFullYear() + "/" + (d.getMonth() + 1) + "/" + d.getDate()
    ) {
        console.log("身份证上的出生日期非法");
        return false;
    }

    // 身份证号码校验
    let sum = 0,
        weights = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2],
        codes = "10X98765432";
    for (let i = 0; i < sId.length - 1; i++) {
        sum += sId[i] * weights[i];
    }
    let last = codes[sum % 11]; //计算出来的最后一位身份证号码
    if (sId[sId.length - 1] != last) {
        console.log("你输入的身份证号非法");
        return false;
    }

    return true;
};

/**
 * @description: 判断是否为16进制颜色，rgb 或 rgba
 * @param { String } str
 * @return { Boolean }
 * @author: 杜康
 * @Date: 2021-08-14 09:50:09 +0800
 */
const isColor = (str) => {
    return /^(#([0-9a-fA-F]{3}){1,2}|[rR][gG][Bb](\((\s*(2[0-4]\d|25[0-5]|[01]?\d{1,2})\s*,){2}\s*(2[0-4]\d|25[0-5]|[01]?\d{1,2})\s*\)|[Aa]\((\s*(2[0-4]\d|25[0-5]|[01]?\d{1,2})\s*,){3}\s*([01]|0\.\d+)\s*\)))$/.test(
        str
    );
};

/**
 * @description: 判断是否为URL地址
 * @param { String } str
 * @return { Boolean }
 * @author: 杜康
 * @Date: 2021-08-14 09:48:55 +0800
 */
const isUrl = (str) => {
    return /[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/i.test(
        str
    );
};

// typeof null 的结果也是 object，数组的结果也是 object
// toString为Object的原型方法，而Array 、Function等类型作为Object的实例，都重写了toString方法 所以obj.toString()的结果和Object.prototype.toString.call(obj)的结果不一样
// 以下 无法区分自定义对象类型，自定义类型可以采用instanceof区分

/**
 * @description: 是否字符串
 * @param {*} o
 * @return {*}
 * @author: 杜康
 * @Date: 2021-08-14 09:59:50 +0800
 */
const isString = (o) => {
    return Object.prototype.toString.call(o).slice(8, -1) === "String";
};

/**
 * @description: 是否数字
 * @param {*} o
 * @return {*}
 * @author: 杜康
 * @Date: 2021-08-14 10:00:05 +0800
 */
const isNumber = (o) => {
    return Object.prototype.toString.call(o).slice(8, -1) === "Number";
};

/**
 * @description: 是否boolean
 * @param {*} o
 * @return {*}
 * @author: 杜康
 * @Date: 2021-08-14 10:00:15 +0800
 */
const isBoolean = (o) => {
    return Object.prototype.toString.call(o).slice(8, -1) === "Boolean";
};

/**
 * @description: 是否函数
 * @param {*} o
 * @return {*}
 * @author: 杜康
 * @Date: 2021-08-14 10:00:25 +0800
 */
const isFunction = (o) => {
    return Object.prototype.toString.call(o).slice(8, -1) === "Function";
};

/**
 * @description: 是否为null
 * @param {*} o
 * @return {*}
 * @author: 杜康
 * @Date: 2021-08-14 10:01:08 +0800
 */
const isNull = (o) => {
    return Object.prototype.toString.call(o).slice(8, -1) === "Null";
};

/**
 * @description: 是否undefined
 * @param {*} o
 * @return {*}
 * @author: 杜康
 * @Date: 2021-08-14 10:01:33 +0800
 */
const isUndefined = (o) => {
    return Object.prototype.toString.call(o).slice(8, -1) === "Undefined";
};

/**
 * @description: 是否对象
 * @param {*} o
 * @return {*}
 * @author: 杜康
 * @Date: 2021-08-14 10:01:42 +0800
 */
const isObj = (o) => {
    return Object.prototype.toString.call(o).slice(8, -1) === "Object";
};

/**
 * @description: 是否数组
 * @param {*} o
 * @return {*}
 * @author: 杜康
 * @Date: 2021-08-14 10:01:52 +0800
 */
const isArray = (o) => {
    return Object.prototype.toString.call(o).slice(8, -1) === "Array";
};

/**
 * @description: 是否时间
 * @param {*} o
 * @return {*}
 * @author: 杜康
 * @Date: 2021-08-14 10:02:02 +0800
 */
const isDate = (o) => {
    return Object.prototype.toString.call(o).slice(8, -1) === "Date";
};

/**
 * @description: 是否正则
 * @param {*} o
 * @return {*}
 * @author: 杜康
 * @Date: 2021-08-14 10:02:13 +0800
 */
const isRegExp = (o) => {
    return Object.prototype.toString.call(o).slice(8, -1) === "RegExp";
};

/**
 * @description: 是否错误对象
 * @param {*} o
 * @return {*}
 * @author: 杜康
 * @Date: 2021-08-14 10:02:26 +0800
 */
const isError = (o) => {
    return Object.prototype.toString.call(o).slice(8, -1) === "Error";
};

/**
 * @description: 是否Symbol函数
 * @param {*} o
 * @return {*}
 * @author: 杜康
 * @Date: 2021-08-14 10:08:59 +0800
 */
const isSymbol = (o) => {
    return Object.prototype.toString.call(o).slice(8, -1) === "Symbol";
};

/**
 * @description: 是否Promise对象
 * @param {*} o
 * @return {*}
 * @author: 杜康
 * @Date: 2021-08-14 10:09:08 +0800
 */
const isPromise = (o) => {
    return Object.prototype.toString.call(o).slice(8, -1) === "Promise";
};

/**
 * @description: 是否Set对象
 * @param {*} o
 * @return {*}
 * @author: 杜康
 * @Date: 2021-08-14 10:09:18 +0800
 */
const isSet = (o) => {
    return Object.prototype.toString.call(o).slice(8, -1) === "Set";
};

/**
 * @description: 是否false
 * @param {*} o
 * @return {*}
 * @author: 杜康
 * @Date: 2021-08-14 10:09:27 +0800
 */
const isFalse = (o) => {
    if (
        o == "" ||
        o == undefined ||
        o == null ||
        o == "null" ||
        o == "undefined" ||
        o == 0 ||
        o == false ||
        o == NaN
    )
        return true;
    return false;
};

/**
 * @description: 是否true
 * @param {*} o
 * @return {*}
 * @author: 杜康
 * @Date: 2021-08-14 10:09:38 +0800
 */
const isTrue = (o) => {
    return !this.isFalse(o);
};

/**
 * @description: 判断是否为数字类型
 * @param {*} value
 * @return {*}
 * @author: 杜康
 * @Date: 2021-08-14 09:49:30 +0800
 */
const isDigit = (value) => {
    let patrn = /^[0-9]*$/;
    if (patrn.exec(value) == null || value == "") {
        return false;
    } else {
        return true;
    }
};

export default {
    isEmail,
    isEmail2,
    isTelCode,
    isPhoneNum,
    isCnNewID,
    isIdCard,
    isCardID,
    isColor,
    isUrl,
    isString,
    isNumber,
    isBoolean,
    isFunction,
    isNull,
    isUndefined,
    isObj,
    isArray,
    isDate,
    isRegExp,
    isError,
    isSymbol,
    isPromise,
    isSet,
    isFalse,
    isTrue,
    isDigit,
};
